package com.oswego.dawcs;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;

public class NumberPickerPreference extends DialogPreference {

	private static final int DEFAULT_MAX = 16;
	private static final int DEFAULT_MIN = 0;
	private static final int DEFAULT_VALUE = 0;

	private int max;
	private int min;
	private int value;
	private NumberPicker picker = null;

	public NumberPickerPreference(Context context, AttributeSet attrs) {
		super(context, attrs);

		setMinValue(DEFAULT_MIN);
		setMaxValue(DEFAULT_MAX);

		setDialogLayoutResource(R.layout.pref_num_picker_dialog);

		setPositiveButtonText("Set");
		setNegativeButtonText("Cancel");
	}

	public void setValue(int num) {
		if (num >= min && num <= max) {
			value = num;
			persistInt(value);
		}
	}

	public void setMinValue(int num) {
		min = num;
	}

	public void setMaxValue(int num) {
		max = num;
	}

	@Override
	protected void onSetInitialValue(boolean restore, Object defaultValue) {
		setValue(restore ? getPersistedInt(DEFAULT_VALUE)
				: (Integer) defaultValue);
	}

	@Override
	protected Object onGetDefaultValue(TypedArray a, int index) {
		return a.getInt(index, DEFAULT_VALUE);
	}

	@Override
	protected void onBindDialogView(View view) {
		super.onBindDialogView(view);

		TextView dialogMessageText = (TextView) view
				.findViewById(R.id.text_dialog_message);
		dialogMessageText.setText(getDialogMessage());

		picker = (NumberPicker) view.findViewById(R.id.pref_num_picker);
		picker.setMinValue(min);
		picker.setMaxValue(max);
		picker.setValue(value);
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		super.onDialogClosed(positiveResult);

		if (positiveResult) {
			int numberPickerValue = picker.getValue();
			if (callChangeListener(numberPickerValue)) {
				setValue(numberPickerValue);
			}
		}
	}

	@Override
	protected Parcelable onSaveInstanceState() {
		final Parcelable superState = super.onSaveInstanceState();

		final SavedState myState = new SavedState(superState);
		myState.minValue = min;
		myState.maxValue = max;
		myState.value = value;

		return myState;
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
		if (state == null || !state.getClass().equals(SavedState.class)) {
			super.onRestoreInstanceState(state);
			return;
		}
		SavedState myState = (SavedState) state;
		min = myState.minValue;
		max = myState.maxValue;
		setValue(myState.value);

		super.onRestoreInstanceState(myState.getSuperState());
	}

	private static class SavedState extends BaseSavedState {
		int minValue;
		int maxValue;
		int value;

		public SavedState(Parcelable superState) {
			super(superState);
		}

		public SavedState(Parcel source) {
			super(source);

			minValue = source.readInt();
			maxValue = source.readInt();
			value = source.readInt();
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			super.writeToParcel(dest, flags);

			dest.writeInt(minValue);
			dest.writeInt(maxValue);
			dest.writeInt(value);
		}
	}
}
